package edu.bw.controller;

import edu.bw.enums.OperationStatus;
import edu.bw.po.Product;
import edu.bw.service.ProductService;
import edu.bw.utils.ResultUtils;
import edu.bw.vo.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @GetMapping("/list")
    public ResponseData findAll() {
        try {
            productService.findAll();
            return ResultUtils.result(OperationStatus.SUCCESS, null);
        } catch (Exception e) {
            return ResultUtils.result(OperationStatus.FAIL, null);
        }
    }
    @PostMapping("/add")
    public ResponseData addProduct(@RequestBody Product product){
        try {
            productService.saveProduct(product);
            return ResultUtils.result(OperationStatus.SUCCESS, null);
        } catch (Exception e) {
            return ResultUtils.result(OperationStatus.FAIL, e.getMessage());
        }
    }

    @PutMapping("/updata")
    public ResponseData updataProduct(@RequestBody Product product) {
        try {
            productService.update(product);
            return ResultUtils.result(OperationStatus.SUCCESS, null);
        } catch (Exception e) {
            return ResultUtils.result(OperationStatus.FAIL, e.getMessage());
        }
    }

    @DeleteMapping("/delete/{pid}")
    public ResponseData deleteProduct(@PathVariable("pid") Integer pid) {
        try {
            productService.deleteByPid(pid);
            return ResultUtils.result(OperationStatus.SUCCESS, null);
        } catch (Exception e) {
            return ResultUtils.result(OperationStatus.FAIL, e.getMessage());
        }
    }
}

